const { countFinance, getFinanceList, addFinanceRecord, removeFinanceRecord, updateFinanceRecord } = require('../../model/finance')
const { getMemberInfoById } = require('../../model/member')
/**
 * 获取资金流水列表(医师) 
 */
const financeList = async (req, res) => {
    const output = { code: 200, msg: '操作成功' }
    let { date, page, size } = req.query
    let startTime = date ? new Date(date).setHours(0, 0, 0) : new Date().setHours(0, 0, 0)
    let endTime = date ? new Date(date).setHours(24, 0, 0) : new Date().setHours(24, 0, 0)
    // 获取登录信息
    const id = req.loginInfo?.id
    const map = {
        doctor_id: id, trade_time: [startTime, endTime]
    }
    // 分页参数
    if (page && size) {
        map.page = page
        map.size = size
    }
    const result = await getFinanceList(map)
    output.data = result
    res.send(output)
}

/**
 * 新增收(退)款记录 
 */
const financeAdd = async (req, res) => {
    const output = { code: 200, msg: '操作成功' }
    const { patient_id, amount, type, origin_id, pay_way } = req.body
    if (!patient_id || !amount || !type || !pay_way) {
        output.code = 500
        output.msg = '参数错误:缺少关键参数patient_id || amount || type || pay_way'
        return res.send(output)
    }
    const trade_time = create_time = Date.now()
    const status = 1
    // 获取登录账号id
    const { id: doctor_id } = req.loginInfo
    const { bus_id } = await getMemberInfoById(doctor_id)
    const data = { bus_id, doctor_id, patient_id, pay_way, amount, type, status, trade_time, create_time }
    if (origin_id) data.origin_id = origin_id
    await addFinanceRecord(data)
    res.send(output)
}


/**
 * 更新收(退)款记录 
 */
const financeUpdate = async (req, res) => {
    const output = { code: 200, msg: '操作成功' }
    const { id, patient_id, amount, type, pay_way, status } = req.body
    if (!id || !patient_id || !amount || !type || !pay_way || !status) {
        output.code = 500
        output.msg = '参数错误:缺少关键参数id || patient_id || amount || type || pay_way || status'
        return res.send(output)
    }
    const data = { patient_id, pay_way, amount, type, status }
    await updateFinanceRecord(id, data)
    res.send(output)
}

/**
 * 删除收(退)款记录 
 */
const financeRemove = async (req, res) => {
    const output = { code: 200, msg: '操作成功' }
    const { id } = req.query
    if (!id) {
        output.code = 500
        output.msg = '参数错误:缺少关键参数id'
        return res.send(output)
    }
    await removeFinanceRecord(id)
    res.send(output)
}

/**
 * 统计当日收款/退款数据()
 * 
*/
const financeAnalysisToday = async (req, res) => {
    const output = { code: 200, msg: '操作成功' }
    const startTime = new Date().setHours(0, 0, 0, 0)
    const endTime = new Date().setHours(24, 0, 0, 0)
    const { doctor_id } = req.loginInfo
    const map = {
        doctor_id,
        trade_time: [startTime, endTime]
    }
    const result = await countFinance(map)
    output.data = result
    res.send(output)
}

module.exports = {
    financeList, financeAdd, financeUpdate, financeRemove, financeAnalysisToday
}